package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;


    // 查询所有的角色数据
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @GetMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> list = roleService.findAll();

            return new Result(true, MessageConstant.GET_ROLE_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLE_FAIL);
        }
    }

    // 角色数据分页查询
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
            return roleService.pageQuery(queryPageBean);
    }

    /**
     * 根据role_id查询基础的角色信息
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role=roleService.findById(id);
            return new Result(true,"查询角色成功",role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询角色失败");
        }
    }
    /**
     * 根据role_id查询关联的执行权限
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findPermissionsByRoleId")
    public Result findPermissionsByRoleId(Integer id){
        try {
            Integer[] permissions=roleService.findPermissionsByRoleId(id);
            return new Result(true,"查询关联执行权限成功",permissions);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询关联执行权限失败");
        }
    }

    /**
     * 根据role_id；查询关联的菜单权限
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findUrlsByRoleId")
    public Result findUrlsByRoleId(Integer id){
        try {
            Integer[] urls=roleService.findUrlsByRoleId(id);
            return new Result(true,"查询关联执行权限成功",urls);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询关联执行权限失败");
        }
    }

    /**
     * 更新角色信息
     * @param urls
     * @param permissions
     * @param role
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    @RequestMapping("/edit")
    public Result edit(Integer[] urls,Integer[] permissions,@RequestBody Role role){
        try{
            roleService.edit(urls,permissions,role);
            return new Result(true,"修改角色数据成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改角色数据失败");
        }
    }

    /**
     * 新增用户信息，需要传递三个参数
     * @param urls
     * @param permissions
     * @param role
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    @RequestMapping("/add")
    public Result addRole(Integer[] urls,Integer[] permissions,@RequestBody Role role){
        try {
            roleService.addRole(role,permissions,urls);
            return new Result(true,"添加用户成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"添加角色失败");
        }
    }

    /**
     * 删除角色信息
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true,"删除角色成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"角色已关联用户,删除角色失败");
        }
    }
}